package org.lanqiao.tjut.entity;

import java.util.List;
import java.util.Scanner;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;
import org.lanqiao.tjut.db.DBDriverFactory;

public class Service {
	@Test
	public void testLogic() {

		System.out.println("--------------用户登录-----------------");
		System.out.println("1、学生登录");
		System.out.println("2、老师登录");
		System.out.println("3、退出");
		Scanner input = new Scanner(System.in);
		System.out.println("请输入登录类型：");
		int i = input.nextInt();
		switch (i) {
		case 1:
			doStudent();
			break;
		case 2:
			Account();
			break;
		case 3:

			System.out.println("退出登录");
			break;
		}
	}

	/**
	 * 老师登录
	 */
	private void Account() {

		Scanner input = new Scanner(System.in);
		System.out.println("请输入账号：");
		String strNumberId = input.next();
		System.out.println("请输入密码：");
		String strPassword = input.next();
		// 账号密码唯一性判定
		if (strNumberId.equals("admin") && strPassword.equals("123456")) {
			doTeacher();

		} else {

			System.out.println("账号或密码错误，请重新登录");
			Account();
		}

	}

	/**
	 * 老师功能菜单表
	 */
	private void doTeacher() {
		System.out.println("----------------------老师功能菜单表-----------------");
		System.out.println("1、添加学生信息");
		System.out.println("2、查询学生信息");
		System.out.println("3、修改学生信息");
		System.out.println("4、删除学生信息");

		System.out.println("0、任意键退出");
		Scanner input = new Scanner(System.in);
		System.out.println("请选择功能菜单：");
		int i1 = input.nextInt();
		switch (i1) {
		case 1:
			// 添加学生信息
			add();
			break;
		case 2:
			// 查询学生信息
			queryFunctionMenu();
			break;
		case 3:
			// 修改学生信息
			modifier();
			break;
		case 4:
			// 删除学生信息
			delete();
			break;

		case 0:
			System.out.println("任意键退出");
			break;
		}

	}

	/**
	 * 添加信息
	 */
	private void add() {
		Scanner input = new Scanner(System.in);
		System.out.println("--------添加学生信息-------------");
		System.out.println("请输入学生学号：");
		int iNumberId = input.nextInt();
		System.out.println("请输入学生姓名：");
		String strName = input.next();
		System.out.println("请输入学生性别：");
		String strSex = input.next();
		System.out.println("请输入学生年龄：");
		int iAge = input.nextInt();
		System.out.println("请输入学生电话：");
		int iPhone = input.nextInt();
		System.out.println("请输入学生密码：");
		int iPassword = input.nextInt();
		System.out.println("请输入学生地址：");
		String strAddress = input.next();
		// 获取数据操作的sql语句
		String strSQL = "INSERT INTO STUDENT" + "(stuNumberId,stuName,stuSex,stuAge,stuPhone,stuPassword,stuAddress)"
				+ "VALUES(?,?,?,?,?,?,?)";
		// 获取结果
		int i1 = DBDriverFactory.getDBDriverInstance().update(strSQL,
				new Object[] { iNumberId, strName, strSex, iAge, iPhone, iPassword, strAddress });
		System.out.println(i1 > 0 ? "新增成功" : "新增失败");
		System.out.println("----------------------------------------------------------------------");
		//返回老师功能菜单表
		doTeacher();
	}

	/**
	 * 查询功能菜单列表
	 */
	public void queryFunctionMenu() {
		Scanner input = new Scanner(System.in);
		System.out.println("1、查询所有学生信息");
		System.out.println("2.根据指定编号(学号)查询单个学生信息");
		System.out.println("3.根据姓名模糊查询学生信息");
		System.out.println("4.根据地址模糊查询学生信息");
		System.out.println("5.根据年龄段查询学生信息");
		System.out.println("6.返回上层菜单");
		int inIput = input.nextInt();
		switch (inIput) {
		case 1:
			// 查询所有学生信息
			queryAllStudentInformation();
			break;
		case 2:
			// 根据指定编号(学号)查询单个学生信息
			queryOneStudentInformation();
			break;
		case 3:
			// 根据姓名模糊查询学生信息
			queryNameStutentInformation();
			break;
		case 4:
			// 根据地址模糊查询学生信息
			queryAddressStudentInformation();
			break;
		case 5:
			// 根据年龄段查询学生信息
			queryAgeStudentInformation();
			break;
		case 6:
			//返回老师功能菜单表
			doTeacher();
			break;
		default:
			System.out.println("输入错误，请重新输入！");
			//返回到查询功能菜单表 
			queryFunctionMenu();
		}
	}

	/**
	 * 查询所有学生信息
	 */
	private void queryAllStudentInformation() {
		Scanner input = new Scanner(System.in);
		String strSql = "SELECT * FROM STUDENT";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());
		printList(lst);
		// 返回上层菜单
		System.out.println("输入-1返回上层功能菜单");
		String strInput = input.next();
		if (strInput.equals("-1")) {
			//返回老师功能菜单表
			doTeacher();
		}
	}

	/**
	 * 根据指定学号查询单个学生信息
	 */
	private void queryOneStudentInformation() {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入需要查询的学生的学号：");
		int iNumberId = input.nextInt();
		String strSQL = "SELECT * FROM STUDENT WHERE STUNUMBERID = ?";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSQL, new MapListHandler(),
				new Object[] { iNumberId });
		printList(lst);
		System.out.println("是否继续根据指定学号查询单个学生信息？(输入-1停止录入并返回上层功能菜单或返回到老师功能菜单表)");
		String strContinue = input.next();
		if (strContinue.equals("-1")) {
			//返回到查询功能菜单表 
			queryFunctionMenu();
		} else {
			//返回老师功能菜单表
			doTeacher();
		}
	}

	/**
	 * 3.根据姓名模糊查询学生信息
	 */
	private void queryNameStutentInformation() {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入需要姓名模糊查询的字：");
		String stuName = input.next();
		stuName = "%" + stuName + "%";
		String strSql = "SELECT * FROM STUDENT WHERE STUNAME LIKE '" + stuName + "'";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());
		printList(lst);
		System.out.println("是否继续根据姓名模糊查询学生信息？(输入-1停止录入并返回上层功能菜单，输入其他键继续)");
		String strContinue = input.next();
		if (strContinue.equals("-1")) {
			//返回到查询功能菜单表 
			queryFunctionMenu();
		} else {
			//返回老师功能菜单表
			doTeacher();
		}
	}

	/**
	 * 4.根据地址模糊查询学生信息
	 */
	private void queryAddressStudentInformation() {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入需要根据地址模糊查询的字：");
		String stuAddress = input.next();
		stuAddress = "%" + stuAddress + "%";
		String strSql = "SELECT * FROM STUDENT WHERE STUADDRESS LIKE '" + stuAddress + "'";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());
		printList(lst);
		System.out.println("是否继续根据地址模糊查询学生信息？(输入-1停止录入并返回上层功能菜单，输入其他键继续)");
		String strContinue = input.next();
		if (strContinue.equals("-1")) {
			//返回到查询功能菜单表 
			queryFunctionMenu();
		} else {
			//返回老师功能菜单表
			doTeacher();
		}
	}

	/**
	 * 根据年龄段查询学生信息
	 */
	private void queryAgeStudentInformation() {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入需要根据年龄段模糊查询的年龄段(较小的年龄)：");
		int stuAgeMin = input.nextInt();
		System.out.println("请输入需要根据年龄段模糊查询的年龄段(较大的年龄)：");
		int stuAgeMax = input.nextInt();
		String strSql = "SELECT * FROM STUDENT WHERE STUAGE >=? AND STUAGE<=?";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler(),
				new Object[] { stuAgeMin, stuAgeMax });
		printList(lst);
		System.out.println("是否继续根据年龄段模糊查询学生信息?(输入-1停止录入并返回上层功能菜单，输入其他键继续)");
		String strContinue = input.next();
		if (strContinue.equals("-1")) {
			//返回到查询功能菜单表 
			queryFunctionMenu();
		} else {
			//返回老师功能菜单表
			doTeacher();
		}
	}

	/**
	 * 使用iterator迭代遍历List集合
	 * 
	 * @param lst
	 */
	public static void printList(List lst) {
		System.out.println("所有的学生信息：");
		for (int i = 0; i < lst.size(); i++) {
			System.out.println(lst.get(i).toString());
		}
	}

	/**
	 * 修改学生信息
	 */
	private void modifier() {

		Scanner input = new Scanner(System.in);
		String strSql = "SELECT * FROM STUDENT";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());
		printList(lst);
		while (true) {
			System.out.println("请输入您要修改的学生学号：");
			int iNumberId = input.nextInt();
			System.out.println("请输入新的学生初始密码：");
			String stuNewPassword = input.next();
			System.out.println("请输入新的学生姓名:");
			String stuNewName = input.next();
			System.out.println("请输入新的学生性别:");
			String stuNewSex = input.next();
			System.out.println("请输入新的学生年龄:");
			int iNewAge = input.nextInt();
			System.out.println("请输入新的学生电话:");
			int iNewPhone = input.nextInt();
			System.out.println("请输入新的学生地址:");
			String stuNewAddress = input.next();
			String strSql1 = "update STUDENT set STUPASSWORD='" + stuNewPassword + "',STUNAME='" + stuNewName
					+ "',STUNUMBERID='" + iNumberId + "',STUSEX='" + stuNewSex + "',STUAGE='" + iNewAge + "',STUPHONE='"
					+ iNewPhone + "',STUADDRESS='" + stuNewAddress + "'  where STUNUMBERID =?";
			int i = DBDriverFactory.getDBDriverInstance().update(strSql1, new Object[] { iNumberId });
			if (i > 0) {
				System.out.println("该学生信息修改成功！");
			} else {
				System.out.println("该学生信息修改失败！");
			}
			String strSql2 = "SELECT * FROM STUDENT";
			List lst1 = DBDriverFactory.getDBDriverInstance().query(strSql2, new MapListHandler());
			printList(lst1);

			System.out.println("是否继续修改其他学生的信息？(输入-1停止修改并返回上层功能菜单，输入其他键继续)");
			String strContinue = input.next();
			if (strContinue.equals("-1")) {
				doTeacher();
				break;
			}
		}
	}

	/**
	 * 删除学生信息
	 */
	private void delete() {

		Scanner input = new Scanner(System.in);
		String strSql = "SELECT * FROM STUDENT";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());
		printList(lst);
		while (true) {
			System.out.println("请输入您要删除的学生学号：");
			int iNumberId = input.nextInt();
			String strSql1 = "DELETE FROM STUDENT WHERE STUNUMBERID=?";
			int i = DBDriverFactory.getDBDriverInstance().update(strSql1, new Object[] { iNumberId });
			if (i > 0) {
				System.out.println("该学生信息删除成功！");
			} else {
				System.out.println("该学生信息删除失败！");
			}
			String strSql2 = "SELECT * FROM STUDENT";
			List lst1 = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());
			printList(lst1);
			System.out.println("是否继续修改其他学生的信息？(输入-1停止删除并返回上层功能菜单，输入其他键继续)");
			String strContinue = input.next();
			if (strContinue.equals("-1")) {
				doTeacher();
				break;
			}
		}

	}

	/**
	 * 学生登录
	 */
	private void doStudent() {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入学生账号：");
		String MyNumberId = input.next();
		String strSQL = "SELECT stuPassword FROM STUDENT WHERE stuNumberId=?";
		// ScalarHandler()作用是查询某一个值
		String passwordNum = (String) DBDriverFactory.getDBDriverInstance().query(strSQL, new ScalarHandler(),
				new Object[] { MyNumberId });
		System.out.println("请输入密码：");
		String MyPassword = input.next();
		// 账号密码唯一性判定
		if (MyPassword.equals(passwordNum)) {
			stuFunction(MyNumberId);
		} else {

			System.out.println("账号或密码错误，请重新登录");
			doStudent();
		}
	}

	/**
	 * 学生功能菜单
	 * 
	 * @param iMyNumberId
	 *            学生学号
	 */
	private void stuFunction(String MyNumberId) {
		System.out.println("1、查询个人信息");
		System.out.println("2、修改密码");
		System.out.println("3、查询同学信息");
		System.out.println("4、添加好友");
		System.out.println("5、显示我的所有好友信息");
		System.out.println("6、保存好友信息");
		System.out.println("7、给好友留言");
		System.out.println("8、查看我的留言板");
		System.out.println("9、删除好友");
		System.out.println("10、修改好友备注");
		System.out.println("0............");
		Scanner input = new Scanner(System.in);
		System.out.println("请选择功能菜单");
		int iFunction = input.nextInt();
		switch (iFunction) {
		case 1:
			//查询个人信息
			myQuery(MyNumberId);
			break;
		case 2:
			//修改密码
			modifierPassword(MyNumberId);
			break;
		case 3:
			//查询同学信息
			queryMyStudentFunctionMenu(MyNumberId);
			break;
		case 4:
			//添加好友
			addFriends();
			break;
		case 5:
			showFriendsInformation();
			break;
		case 9:
			deleteFriends();
			break;
		case 10:
		
			System.out.println("退出登录'");
			break;
		}

	}

	/**
	 * 查询个人信息
	 */
	private void myQuery(String MyNumberId) {
		String strSql = "SELECT * FROM STUDENT WHERE STUNUMBERID= ?";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler(),
				new Object[] { MyNumberId });

		printList01(lst);
		System.out.println("-------------------------------------------------------------");
		stuFunction(MyNumberId);

	}

	/**
	 * 使用iterator迭代遍历List集合
	 * 
	 * @param lst
	 */
	public static void printList01(List lst) {
		System.out.println("我的个人信息：");
		for (int i = 0; i < lst.size(); i++) {
			System.out.println(lst.get(i).toString());
		}
	}

	/**
	 * 修改学生个人密码
	 * 
	 * @param iMyNumberId
	 *            学生密码
	 */
	public void modifierPassword(String MyNumberId) {
		System.out.println("------------修改密码-----------------");
		Scanner input = new Scanner(System.in);
		System.out.println("请输入你的默认原始密码：");
		String MyOldPassword = input.next();
		// 获取数据操作的sql语句
		String strSQL = "SELECT stuPassword FROM STUDENT WHERE STUNUMBERID=?";
		// 查询数据库存储的密码
		String MyOldPassword1 = (String) DBDriverFactory.getDBDriverInstance().query(strSQL, new ScalarHandler(),
				new Object[] { MyNumberId });
		// 判定输入的默认原始密码是否和数据库存储的密码一致
		if (MyOldPassword.equals(MyOldPassword1)) {
			System.out.println("请输入新密码：");
			String MyNewPassword = input.next();
			System.out.println("请再次输入你的新密码：");
			String MyNewPassword1 = input.next();
			//判断两次输入的新密码是否相同
			if (MyNewPassword.equals(MyNewPassword1)) {
				String strSQL1 = "Update STUDENT SET stuPassword=  '" + MyNewPassword + "' WHERE stuNumberId=? ";
				int i = DBDriverFactory.getDBDriverInstance().update(strSQL1, new Object[] { MyNumberId });
				System.out.println(i > 0 ? "修改成功" : "修改失败");
			} else {
				System.out.println("你两次输入的新密码不一致，请重新输入");
				modifierPassword(MyNumberId);
			}
		} else {
			System.out.println("你输入的默认原始密码不正确，请重新输入");
			modifierPassword(MyNumberId);
		}
		System.out.println("-----------------------------");
		stuFunction(MyNumberId);
	}

	/**
	 * 3.学生查询功能菜单
	 * 
	 * @param inputStuid
	 *            学号
	 */
	public void queryMyStudentFunctionMenu(String MyNumberId) {
		Scanner input = new Scanner(System.in);
		System.out.println("1.查询所有学生信息");
		System.out.println("2.根据指定学号查询单个学生信息");
		System.out.println("3.根据姓名模糊查询学生信息");
		System.out.println("4.根据地址模糊查询学生信息");
		System.out.println("5.根据年龄段查询学生信息");
		System.out.println("6.返回上层菜单");
		int i = input.nextInt();
		switch (i) {
		case 1:
			// 查询所有学生信息
			selectAllStudent(MyNumberId);
			break;
		case 2:
			// 根据指定学号查询单个学生信息
			selectOneStudent(MyNumberId);
			break;
		case 3:
			// 根据姓名模糊查询学生信息
			selectStudentName(MyNumberId);
			break;
		case 4:
			// 根据地址模糊查询学生信息
			selectStudentAddress(MyNumberId);
			break;
		case 5:
			// 根据年龄段查询学生信息
			selectStudentAge(MyNumberId);
			break;
		case 6:
			stuFunction(MyNumberId);
			break;
		default:
			System.out.println("输入错误，请重新输入！");
			queryMyStudentFunctionMenu(MyNumberId);
		}
	}

	/**
	 * 查询我的所有同学
	 */
	private void selectAllStudent(String MyNumberId) {
		Scanner input = new Scanner(System.in);
		String strSql = "SELECT stuNumberID,stuName,stuSex,stuAge,stuPhone,stuAddress FROM STUDENT";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());

		printList(lst);
		// 返回上层菜单
		System.out.println("输入-1返回上层功能菜单");
		String strInput = input.next();
		if (strInput.equals("-1")) {
			queryMyStudentFunctionMenu(MyNumberId);
		}
	}

	/**
	 * 2.根据学号查询一个同学信息
	 */
	public void selectOneStudent(String MyNumberId) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入您想要查询的同学的学号：");
		String MyStudentNumberId = input.next();
		String strSql = "SELECT stuNumberID,stuName,stuSex,stuAge,stuPhone,stuAddress FROM STUDENT WHERE STUNUMBERID=?";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler(),
				new Object[] { MyNumberId });

		printList(lst);
		System.out.println("是否继续其他学生信息？(输入-1停止录入并返回上层功能菜单，输入其他键继续)");
		String strInput = input.next();
		if (strInput.equals("-1")) {
			queryMyStudentFunctionMenu(MyNumberId);
		} else {
			stuFunction(MyNumberId);
		}

	}

	/**
	 * 3.根据姓名模糊查询学生信息
	 */
	public void selectStudentName(String MyNumberId) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入需要姓名模糊查询的字：");
		String stuName = input.next();
		stuName = "%" + stuName + "%";
		String strSql = "SELECT stuNumberID,stuName,stuSex,stuAge,stuPhone,stuAddress   FROM STUDENT WHERE STUNAME LIKE '"
				+ stuName + "'";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());

		printList(lst);
		System.out.println("是否继续根据姓名模糊查询学生信息？(输入-1停止录入并返回上层功能菜单，输入其他键继续)");
		String strInput = input.next();
		if (strInput.equals("-1")) {
			queryMyStudentFunctionMenu(MyNumberId);
		} else {
			stuFunction(MyNumberId);
		}
	}

	/**
	 * 4.根据地址模糊查询学生信息
	 * 
	 * @param inputStuid
	 *            学号
	 */
	public void selectStudentAddress(String MyNumberId) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入需要根据地址模糊查询的字：");
		String stuAddress = input.next();
		stuAddress = "%" + stuAddress + "%";
		String strSql = "SELECT  stuNumberID,stuName,stuSex,stuAge,stuPhone,stuAddress FROM STUDENT WHERE STUADDRESS LIKE '"
				+ stuAddress + "'";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());

		printList(lst);
		System.out.println("是否继续根据地址模糊查询学生信息？(输入-1停止录入并返回上层功能菜单，输入其他键继续)");
		String strInput = input.next();
		if (strInput.equals("-1")) {
			queryMyStudentFunctionMenu(MyNumberId);
		} else {
			stuFunction(MyNumberId);
		}
	}

	/**
	 * 5.根据年龄段模糊查询学生信息
	 * 
	 * @param inputStuid
	 *            学号
	 */
	public void selectStudentAge(String MyNumberId) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入需要根据年龄段模糊查询的年龄段：");
		int studentAgemin = input.nextInt();
		System.out.println("请输入需要根据年龄段模糊查询的年龄段：");
		int studentAgemax = input.nextInt();
		String strSql = "SELECT stuNumberID,stuName,stuSex,stuAge,stuPhone,stuAddress FROM STUDENT WHERE STUAGE >=? AND STUAGE<=?";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler(),
				new Object[] { studentAgemin, studentAgemax });

		printList(lst);
		System.out.println("是否继续根据年龄段模糊查询学生信息?(输入-1停止录入并返回上层功能菜单，输入其他键继续)");
		String strInput = input.next();
		if (strInput.equals("-1")) {
			queryMyStudentFunctionMenu(MyNumberId);
		} else {
			stuFunction(MyNumberId);
		}
	}

	private void addFriends() {
	}

	private void showFriendsInformation() {
	}

	private void deleteFriends() {
	}

	private void modifierRemark() {
	}

}
